package org.mg.frontend.security;

import org.mg.core.entity.Role;
import org.mg.core.entity.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author MG
 * @since 06-04-2013
 */
public class AuthUserDetails implements UserDetails {

    private User user;
    private Set<AuthGrantedAuthority> authorities = new HashSet<AuthGrantedAuthority>();

    public AuthUserDetails(User user) {
        this.user = user;
        for (Role role : user.getRoles()) {
            authorities.add(new AuthGrantedAuthority(role));
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
